package com.project.controller;

import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("session")
public class SessionController {

    @RequestMapping("test")
    public String testSession(HttpServletRequest request){
        //得到session会话对象
        HttpSession session = request.getSession();
        //在session对象中绑定用户信息
        session.setAttribute("loginName","tom");

        return "ok";
    }

    @RequestMapping("mytest")
    public String mytest(HttpServletRequest request){
        HttpSession session =  request.getSession();
        //得到session对象中绑定的用户信息
        String value = (String)session.getAttribute("loginName");
        System.out.println(session.getId());

        return value;
    }

    @RequestMapping("add")
    public String add(String name,String userCode,HttpServletRequest request){
        //得到session会话对象
        HttpSession session = request.getSession();
        //从session中取出产生的验证码
        String code = (String)session.getAttribute("checkCode");
        //比较用户输入的验证码和系统验证码是否相等
        if(userCode.equalsIgnoreCase(code) == false){
            return "codeError";
        }
        System.out.println(name);

        return "ok";
    }
}
